package com.acme.bankapp.domain;

import com.acme.bankapp.exceptions.NotEnoughFundsException;
import com.acme.bankapp.exceptions.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount{

	private double overdraft;
	
	public double getOverdraft() {
		return overdraft;
	}


	public CheckingAccount(final double amount, final double overdraft) {
		super(amount);
		this.overdraft = overdraft;
	}

	
	public void withdraw(final double amount) throws NotEnoughFundsException  {
		if (amount < 0) {
			throw new IllegalArgumentException("Amount can not be negative");
		}
		if (this.balance - amount >= -overdraft) {
			this.balance -= amount;
		} else {
			throw new NotEnoughFundsException("amount: "+amount+" balance: "+balance);
		}

		if (this.balance < amount) {
			double overdraftNeeded = amount - this.balance;
			if (overdraft < overdraftNeeded) {
				throw new OverDraftLimitExceededException("amount: "+amount+" balance: "+balance);
			} else {
				this.balance = 0.0;
				overdraft -= overdraftNeeded;
			}
		} else {
			this.balance = this.balance - amount;
		}


	}

	public void deposit(final double amount) throws NotEnoughFundsException {
		if (amount > 0) {
			this.balance += amount;
		} else {
			throw new NotEnoughFundsException("amount: "+amount+" balance: "+balance);
		}

	}

	public double maximumAmountToWithdraw() {
		return this.balance + this.overdraft;
	}


	public String toString() {
		return "Checking "+super.toString()+ "\n[overdraft=" + overdraft + "]";
	}
	
}
